<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_mayizlb/model')->方法名();
*/
namespace jy_mayizlb;
defined('BY_JYA') or exit('error');
require_once ROOT_PLUGIN.'jy_mayizlb/bot/vendor/autoload.php';
use \AntChain\BOT\Client as AntChainBOTClient;
use \AntChain\BOT\Models\Config;
use \AntChain\BOT\Models\QueryLeaseRiskRequest;
use \AntChain\BOT\Models\CreateLeaseRealpersonRequest;
use \AntChain\BOT\Models\QueryLeaseRealpersonRequest;
class model extends \plugin{
	/**
     * 使用AK&SK初始化账号Client
     * @param string $accessKeyId
     * @param string $accessKeySecret
     * @return AntChainBOTClient Client
     */
    public static function createClient(){
        $config = new Config([]);
		$setting=plugin('jy_mayizlb')->plugin_setting('basic');
		if(!$setting['appid'] || !$setting['secret']){
			exi('请联系管理员，设置基本参数！','error');
		}
        // 您的AccessKey ID
        $config->accessKeyId = $setting['appid'];
        // 您的AccessKey Secret
        $config->accessKeySecret = $setting['secret'];
        return new AntChainBOTClient($config);
    }
	/*
		租赁智能中心风控查询服务
	*/
	public function risk_check($post=array()){
		if(!$post['mobile'] || !$post['user_name'] || !$post['cert_no']){
			exi('姓名，身份证号，手机号必填！','error');
		}
		$client = self::createClient();
        $queryLeaseRiskRequest = new QueryLeaseRiskRequest([
			"productInstanceId" => 'bot-aimetaprod-prod',
            "certNo" => $post['cert_no'],
            "userId" => $post['user_id'],
            "userName" => $post['user_name'],
            "ip" => $post['ip']?:getip(),
            "mobile" => $post['mobile'],
        ]);
        $result = $client->queryLeaseRisk($queryLeaseRiskRequest);
		$result=(array)$result;
		//print_r($result);exit;
		if($result['resultCode']!='OK'){
			exi('风控查询失败，'.$result['resultCode'].':'.$result['resultMsg'].',reqMsgId:'.$result['reqMsgId'],'error');
		}
		return json_decode($result['data'],true);
	}
	/*
		实人认证接入
	*/
	public function person_check($post=array()){
		if(!$post['user_name'] || !$post['cert_no']){
			exi('姓名，身份证号必填！','error');
		}
		$client = self::createClient();
		$createLeaseRealpersonRequest = new CreateLeaseRealpersonRequest([
            "certType" => "IDENTITY_CARD",
            "certName" => $post['user_name'],
            "certNo" => $post['cert_no'],
            "outerOrderNo" => $post['ordersn']?:'sn20220701000000',
            "returnUrl" => $post['backurl']?:custom_url('jy_mayizlb/index/backurl/logid='.$post['logid'],true),
            "identityType" => "CERT_INFO"
        ]);
        $result = $client->createLeaseRealperson($createLeaseRealpersonRequest);
		$result=(array)$result;
		//file_put_contents(ROOT_D.'/2.txt',ijson_encode($result));
		if($result['resultCode']!='OK'){
			exi('实人认证接入失败，'.$result['resultCode'].':'.$result['resultMsg'].',reqMsgId:'.$result['reqMsgId'],'error');
		}
		return json_decode($result['data'],true);		
	}
	/*
		查询认证
	*/
	public function person_passed($post=array()){
		if(!$post['certifyId'] || !$post['outerOrderNo']){
			exi('certifyId,outerOrderNo必填！','error');
		}
		$client = self::createClient();
		$queryLeaseRealpersonRequest  = new QueryLeaseRealpersonRequest([
            "certifyId" => $post['certifyId'],
            "outerOrderNo" => $post['outerOrderNo'],
        ]);
        $result = $client->queryLeaseRealperson($queryLeaseRealpersonRequest);
		$result=(array)$result;
		//print_r($result);exit;
		if($result['resultCode']!='OK'){
			exi('查询认证失败，'.$result['resultCode'].':'.$result['resultMsg'].',reqMsgId:'.$result['reqMsgId'],'error');
		}
		return $result['data'];		
	}
}
?>